/**
 * 
 */
package edu.ou.cs.youming;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.Iterator;

import javax.xml.bind.JAXBException;

import org.eclipse.persistence.oxm.MediaType;

import edu.ou.cs.youming.jaxb.types.Example;
import edu.ou.cs.youming.jaxb.types.Label;
import edu.ou.cs.youming.services.DataIO;
import edu.ou.cs.youming.services.JAXBService;

/**
 * @author Youming Lin
 * 
 */
public final class Serializer {
	private static final String corpusPath = "D:/Cruzer USB/Fall 2012/CS 5033/Project/corpus.csv";
	private static final String dir = "D:/Cruzer USB/Fall 2012/CS 5033/Project/rawdata/";
	private static final EnumSet<Label> labels = EnumSet.of(Label.POSITIVE,
			Label.NEGATIVE);

	/**
	 * @param args
	 * @throws JAXBException
	 * @throws IOException
	 */
	public static final void main(final String[] args) throws IOException, JAXBException {
		// create new directory for saving processed data
		final File folder = new File("processeddata");
		folder.mkdir();

		// //////////////////////////////////////////////////////////////////////////////
		// parse data set
		final long startTime = System.nanoTime();
		final ArrayList<Example> examples = (ArrayList<Example>) DataIO.parseExamples(
				corpusPath, dir);
		final long endTime = System.nanoTime();

		System.out.println("Parsing " + examples.size() + " examples took "
				+ (endTime - startTime) / 1000000000.0 + "s.");

		// //////////////////////////////////////////////////////////////////////////////
		// keep only POSITIVE and NEGATIVE labels
		for (final Iterator<Example> itr = examples.iterator(); itr.hasNext();) {
			if (!labels.contains(itr.next().polarity)) {
				itr.remove();
			}
		}

		JAXBService.write(examples, new File(folder.getAbsolutePath() + File.separator
				+ "examples.xml"), MediaType.APPLICATION_XML, true);
	}
}